package com.example.myapplication;


import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;

public class SocketClient extends WebSocketClient {
    private static final String TAG = "socketClient";
    SocketClientListener socketClientListener;

    public SocketClient(URI serverUri) {
        super(serverUri);
    }

    public void setSocketClientListener(SocketClientListener listener) {
        this.socketClientListener = listener;
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        if (socketClientListener != null) {
            socketClientListener.onOpen(handshakedata);
        }
    }

    @Override
    public void onMessage(ByteBuffer bytes) {
        super.onMessage(bytes);
        if (socketClientListener != null) {
            socketClientListener.onMessage(bytes);
        }
    }

    @Override
    public void onMessage(String message) {
        if (socketClientListener != null) {
            socketClientListener.onMessage(message);
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        if (socketClientListener != null) {
            socketClientListener.onClose(code, reason, remote);
        }
    }

    @Override
    public void onError(Exception ex) {
        if (socketClientListener != null) {
            socketClientListener.onError(ex);
        }
    }
}
